package com.supor.aiot.common.ui.view;

import android.net.http.AndroidHttpClient;

import com.alipay.mobile.nebulaappproxy.api.TinyAppRequestPluginProvider;
import com.android.baseconfig.common.utils.Logc;

import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2021, by het, ShenZhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: TinyAppRequestPluginProviderImp <br>
 * 作者: lei <br>
 * 日期: 2021/11/19 <br>
 */
public class TinyAppRequestPluginProviderImp implements TinyAppRequestPluginProvider {
    private static final String TAG = "TinyAppRequestPluginProviderImp";

    private String schemeName = "https";
    private int port = 443;

    public TinyAppRequestPluginProviderImp() {
    }

    public TinyAppRequestPluginProviderImp(String schemeName, int port) {
        this.schemeName = schemeName;
        this.port = port;
    }

    @Override
    public void onAndroidHttpClientCreate(AndroidHttpClient androidHttpClient) {
        KeyStore trustStore;
        try {
            trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            trustStore.load(null, null);

            SSLSocketFactoryImp sf = new SSLSocketFactoryImp(trustStore);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            androidHttpClient.getConnectionManager().getSchemeRegistry().register(new Scheme(schemeName, sf, port));
        } catch (Exception e) {
            Logc.e(TAG, "error in setting trust store", e);
        }
    }

    private class SSLSocketFactoryImp extends SSLSocketFactory {

        SSLContext sslContext = SSLContext.getInstance("TLS");

        public SSLSocketFactoryImp(KeyStore truststore)
                throws NoSuchAlgorithmException, KeyManagementException,
                KeyStoreException, UnrecoverableKeyException {
            super(truststore);

            TrustManager tm = new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                @Override
                public void checkClientTrusted(
                        java.security.cert.X509Certificate[] chain, String authType)
                        throws java.security.cert.CertificateException {
                }

                @Override
                public void checkServerTrusted(
                        java.security.cert.X509Certificate[] chain, String authType)
                        throws java.security.cert.CertificateException {
                }
            };
            sslContext.init(null, new TrustManager[]{tm}, null);
        }

        @Override
        public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
            return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
        }

        @Override
        public Socket createSocket() throws IOException {
            return sslContext.getSocketFactory().createSocket();
        }
    }
}
